package stock_controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import stock_service.WareHouseService;
import stock_vo.CheckedContents;
import stock_vo.WareHouseVO;

public class WareHouseController extends MultiActionController{
	private WareHouseService warehouseService;

	public void setWarehouseService(WareHouseService warehouseService) {
		this.warehouseService = warehouseService;
	}
	public ModelAndView searchFormWareHouse(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println("searchFormWareHouse controller");
		ModelAndView mv = new ModelAndView("stock/request/warehouse_select_request.tiles");
		return mv;
	}
	public ModelAndView insertWareHouse(HttpServletRequest request,
			HttpServletResponse response, WareHouseVO vo) throws Exception{
		ModelAndView mv = new ModelAndView("whjsonView");
		System.out.println("insertWareHouse controller");
		try{
			warehouseService.insertWareHouse(vo);
			List list = warehouseService.selectWareHouse();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	public ModelAndView deleteWareHouse(HttpServletRequest request,
			HttpServletResponse response, CheckedContents contents) throws Exception{
		ModelAndView mv = new ModelAndView("whjsonView");
		System.out.println("deleteWareHouse controller");
		String c[] = contents.getContents();
		System.out.println(c.length);
		try{
			for(int i=0 ; i<c.length ;i++){
				String warehouse_code = c[i];
				System.out.println("warehouse_code = "+warehouse_code);
				warehouseService.deleteWareHouse(warehouse_code);
			}
			List list = warehouseService.selectWareHouse();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	
	public ModelAndView selectWareHouse(HttpServletRequest request,
			HttpServletResponse response, WareHouseVO vo) throws Exception{
		System.out.println("selectWareHouse controller");
		String search = request.getParameter("search");
		String url = "";
		System.out.println(search);
		ModelAndView mv=new ModelAndView();
		try{
			List list = warehouseService.selectWareHouse();
			mv.addObject("list", list);
			System.out.println(list.size());
			url="stock/request/warehouse_insert_request.tiles";
		}catch(SQLException se){
			mv.addObject("info", se.getMessage());
			se.printStackTrace();
		}
		if(search!=null){
			url="warehousePopup";
		}
		mv.setViewName(url);
		return mv;
	}
	public ModelAndView registFormWareHouse(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println("registFormWareHouse controller");
		ModelAndView mv = new ModelAndView("whjsonView");
		return mv;
	}
	
	public ModelAndView checkWareHouse(HttpServletRequest request,
			HttpServletResponse response, WareHouseVO vo) throws Exception{
		List list = warehouseService.checkWareHouse(vo.getWarehouse_name());
		System.out.println("checkWareHouse controller"+list);
		return new ModelAndView("checkwarehousePopup","list",list);
	}
}